package com.nanjia.boot.myflow.outboundorderdetail.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.myflow.outboundorderdetail.model.form.OutboundOrderDetailForm;
import com.nanjia.boot.myflow.outboundorderdetail.model.query.OutboundOrderDetailQuery;
import com.nanjia.boot.myflow.outboundorderdetail.model.vo.OutboundOrderDetailVO;
import com.nanjia.boot.myflow.outboundorderdetail.service.OutboundOrderDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 出库明细前端控制层
 *
 * @author 周洋
 * @since 2025-08-22 18:01
 */
@Tag(name = "出库明细接口")
@RestController
@RequestMapping("/api/v1/outbound-order-detail")
@RequiredArgsConstructor
public class OutboundOrderDetailController  {

    private final OutboundOrderDetailService outboundOrderDetailService;

    @Operation(summary = "出库明细分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('outboundorderdetail:outbound-order-detail:query')")
    public PageResult<OutboundOrderDetailVO> getOutboundOrderDetailPage(OutboundOrderDetailQuery queryParams ) {
        IPage<OutboundOrderDetailVO> result = outboundOrderDetailService.getOutboundOrderDetailPage(queryParams);
        return PageResult.success(result);
    }

//    @Operation(summary = "新增出库明细")
//    @PostMapping
//    @PreAuthorize("@ss.hasPerm('outboundorderdetail:outbound-order-detail:add')")
//    public Result<Void> saveOutboundOrderDetail(@RequestBody @Valid OutboundOrderDetailForm formData , HttpServletRequest request) {
//        return outboundOrderDetailService.saveOutboundOrderDetail(formData, IPUtils.getIpAddr(request));
//
//    }

    @Operation(summary = "获取出库明细表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('outboundorderdetail:outbound-order-detail:edit')")
    public Result<OutboundOrderDetailForm> getOutboundOrderDetailForm(
        @Parameter(description = "出库明细ID") @PathVariable Long id
    ) {
        OutboundOrderDetailForm formData = outboundOrderDetailService.getOutboundOrderDetailFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改出库明细")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('outboundorderdetail:outbound-order-detail:edit')")
    public Result<Void> updateOutboundOrderDetail(
            @Parameter(description = "出库明细ID") @PathVariable Long id,
            @RequestBody @Validated OutboundOrderDetailForm formData
    ) {
        boolean result = outboundOrderDetailService.updateOutboundOrderDetail(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除出库明细")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('outboundorderdetail:outbound-order-detail:delete')")
    public Result<Void> deleteOutboundOrderDetails(
        @Parameter(description = "出库明细ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = outboundOrderDetailService.deleteOutboundOrderDetails(ids);
        return Result.judge(result);
    }
}
